﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _1._3
{
    public partial class Form1 : Form
    {
        private Random random = new Random();
        private int correctAnswer;
        private int _score = 0;
        private int totalQuestions = 10;
        private int timeTick = 100;
        private int timeLimit = 10000; // 每题的时间限制（毫秒）
        private int currentQuestion = 0;
        private int countDown = 0; // 倒计时
        public Form1()
        {
            InitializeComponent();
            InitializeQuiz();
            timer1.Elapsed+=timer1_Tick;
        }
        private void InitializeQuiz()
        {
            _score = 0;
            currentQuestion = 0;
            feedbackLabel.Text = "";
            scoreLabel.Text = $"得分: {_score}";
            NextQuestion();
        }
        private void NextQuestion()
        {
            if (currentQuestion >= totalQuestions)
            {
                MessageBox.Show($"测试结束！你的得分是: {_score}");
                return;
            }
            countDown = 0;
            currentQuestion++;
            GenerateQuestion();
            timer1.Start(); // 开始计时
        }

        private void GenerateQuestion()
        {
            int num1 = random.Next(1, 101);
            int num2 = random.Next(1, 101);
            char op = random.Next(0, 2) == 0 ? '+' : '-';

            if (op == '+')
                correctAnswer = num1 + num2;
            else
                correctAnswer = num1 - num2;

            questionLabel.Text = $"{num1} {op} {num2} = ?";
            answerTextBox.Clear();
            answerTextBox.BackColor = Color.White; // 重置背景色
        }
        private void submitButton_Click(object sender, EventArgs e)
        {


            int userAnswer;
            bool isNumber = int.TryParse(answerTextBox.Text, out userAnswer);
            if (!isNumber)
            {
                feedbackLabel.Text = "请输入一个有效的数字！";
                feedbackLabel.ForeColor = Color.Red;
                return;
            }
            timer1.Stop(); // 停止计时
            if (userAnswer == correctAnswer)
            {
                feedbackLabel.Text = "正确！";
                feedbackLabel.ForeColor = Color.Green;
                _score+=countDown;
            }
            else
            {
                feedbackLabel.Text = "错误！";
                feedbackLabel.ForeColor = Color.Red;
            }

            scoreLabel.Text = $"得分: {_score}";
            NextQuestion();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // 计时器
            timeLabel.Text= $"{(100 - countDown)/10}s";
            if (countDown>=100)
            {
                feedbackLabel.Text = "时间到！";
                feedbackLabel.ForeColor = Color.Red;
                timer1.Stop();
                NextQuestion();
                return;
            }
            countDown++;
        }
    }
}